package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.EmployeeQuery;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    // 显示列表
    @RequestMapping("/list")
    @RequiresPermissions(value = {"employee:list", "员工列表"}, logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") EmployeeQuery qo) {
        //部门下拉框：显示部门的信息
        model.addAttribute("ds", departmentService.listAll());
        PageInfo result = employeeService.query(qo);
        model.addAttribute("result", result);
        return "employee/list"; //跳转到员工的视图
    }


    @RequestMapping("/input")
    @RequiresPermissions(value = {"employee:input", "员工编辑"}, logical = Logical.OR)
    public String input(Long id, Model model) {
        //查询全部角色，然后共享
        model.addAttribute("roles", roleService.listAll());
        //编辑状态下的部门回显: 在input界面或取遍历显示
        model.addAttribute("departments", departmentService.listAll());
        if (id != null) { // 编辑
            Employee employee = employeeService.get(id);
            model.addAttribute("employee", employee);
        }

        return "employee/input";
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"employee:saveOrUpdate", "员工保存或更新"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] ids) {
        //通过 id 判断是添加还是修改
        if (employee.getId() == null) {//添加
            employeeService.save(employee, ids);
        } else {//编辑
            employeeService.update(employee, ids);
        }
        return new JsonResult();
    }


    @RequestMapping("/delete")
    @RequiresPermissions(value = {"employee:delete", "员工删除"}, logical = Logical.OR)
    public String delete(Long id) {
        if (id != null) {
            IEmployeeService employeeService = this.employeeService;
            employeeService.delete(id);
        }

        return "redirect:/employee/list.do";
    }

    @RequestMapping("/batchDelete")
    @RequiresPermissions(value = {"employee:batchDelete", "员工批量删除"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult batchDelete(Long[] ids) {
        employeeService.batchDelete(ids);
        return new JsonResult();
    }

    /**
     * 用户远程验证
     *
     * @param name
     * @return 要求返回值是键值对："valid":true
     */
    @RequestMapping("checkName")
    @ResponseBody
    public Map<String, Boolean> cheackName(String name) {
        Employee employee = employeeService.selectByName(name);
        Map<String, Boolean> map = new HashMap<>();
        map.put("valid", employee == null);
        return map;
    }

    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws Exception {
        //设置文件下载的响应头——>设置下载文件的名称
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");

        //创建工作簿
        //Workbook wb = new HSSFWorkbook();

        //创建Excel文件
        Workbook wb = employeeService.exportXls();


        //把Excel中的数据输出给浏览器
        wb.write(response.getOutputStream());
    }

    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws Exception {
        //调用业务层的方法完成上传
        employeeService.importXls(file);
        return new JsonResult();
    }


}
